package com.example.project22_editingmediaplayer;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowSong;
import com.example.project22_editingmediaplayer.adapter.RowSongplayallsong;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

public class LayoutAllSong extends Fragment {
	public static ListView listview_allsong;
	private static RowSongplayallsong rowAdapter;
	private ArrayList<SongObject> listSong;
	private SongObject song;
	private AudioUltility audioUltility;
	private int indexSong;
	private boolean isService = false;
	public static LoadAllMusic LAM;
	private Activity myActivity;

	Context myContext;

	public static Fragment newInstance(Context context) {
		LayoutAllSong f = new LayoutAllSong();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_allsong,
				null);
		myContext = container.getContext();
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		listview_allsong = (ListView) getActivity().findViewById(
				R.id.listAllSongs);
		myActivity=this.getActivity();
		audioUltility = new AudioUltility(myContext);
		listSong = new ArrayList<SongObject>();
		rowAdapter = new RowSongplayallsong(listSong, this.getActivity());
		listview_allsong.setAdapter(rowAdapter);
		listview_allsong.setOnItemLongClickListener(onItemClick);
		LAM = new LoadAllMusic();
		LAM.execute();

		Bundle extras = getActivity().getIntent().getExtras();
		try {
			indexSong = extras.getInt("index");
			song = listSong.get(indexSong);
			Log.e("isService", String.valueOf(isService));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	OnItemLongClickListener onItemClick = new AdapterView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, long arg3) {

			SongResource.listSong = rowAdapter.getAllData();
			MainActivity.mService.PlaySong(position);
			MainActivity.mService.showNotification(position,
					SongResource.listSong.get(position));
			MainActivity.btnPlay
					.setImageResource(R.drawable.apollo_holo_light_pause);
			MainActivity.namesong.setText(SongResource.listSong.get(position)
					.get_name());
			MainActivity.singer.setText(SongResource.listSong.get(position)
					.get_artist());
			MainActivity.mService.flag_first = false;
			View view1 = LayoutAllSong.listview_allsong
					.getChildAt(MainActivity.mService.getIndex());
			return false;
		}
	};

	public static ArrayList<SongObject> getSongSelected() {
		ArrayList<SongObject> listsongselected = new ArrayList<SongObject>();
		SongResource.deletelist.clear();
		for (int i = 0; i < listview_allsong.getCount(); i++) {
			SongObject s = new SongObject();
			s = (SongObject) listview_allsong.getItemAtPosition(i);
			if (s.isCheck()) {
				listsongselected.add(s);
				SongResource.deletelist.add(i);
				Log.d("get id of selected objectsong",
						String.valueOf(s.get_id()));
			}
		}
		return listsongselected;
	}
//	@Override
//	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//		// TODO Auto-generated method stub
//		super.onCreateOptionsMenu(menu, inflater);
//		menu.clear();
//		inflater.inflate(R.menu.menuallsong, menu);
//	}
//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		// TODO Auto-generated method stub
//		
//		switch (item.getItemId()) {
//		case R.id.deleteSongInStorage:
//			new Delete().execute();
//			break;
//
//		default:
//			break;
//		}
//		return super.onOptionsItemSelected(item);
//		
//	}
//	public class Delete extends AsyncTask<Void, Void, ArrayList<SongObject>>{
//
//		@Override
//		protected ArrayList<SongObject> doInBackground(Void... arg0) {
//			// TODO Auto-generated method stub
//			ArrayList<SongObject> listSongDelete = new ArrayList<SongObject>();
//			listSongDelete =getSongSelected();
//			deleteSongInStorage(listSongDelete);
//			return null;
//		}
//	}
//	public void deleteSongInStorage(ArrayList<SongObject> ls) {
//		AudioUltility au = new AudioUltility(myContext);
//		try {
//			for (int i = 0; i < ls.size(); i++) {
//				File file = new File(au.getOneSongById(ls.get(i).get_id())
//						.get_path());
//				file.delete();
//				Intent intentDelete = new Intent();
//				intentDelete.setAction(MainActivity.DELETE_SONG);
//				intentDelete.putExtra("idDelete", ls.get(i).get_id());
//				//startService(intentDelete);
//				au.deleteSongInMediaStore(ls.get(i).get_id());
//				for (int j = 0; j < SongResource.sharePrefList.size(); j++) {
//					if (ls.get(i).get_id() == SongResource.sharePrefList.get(j)) {
//						SongResource.sharePrefList.remove(j);
//					}
//				}
//				for (int j = 0; j < SongResource.listSong.size(); j++) {
//					if (ls.get(i).get_id() == SongResource.listSong.get(j)
//							.get_id()) {
//						SongResource.listSong.remove(j);
//					}
//				}
//			}
//			Toast toast = Toast.makeText(myActivity, "deleted!",
//					Toast.LENGTH_SHORT);
//			toast.show();
//
//		} catch (Exception e) {
//			// TODO: handle exception
//			Toast toast = Toast.makeText(myActivity, "no song deleted!",
//					Toast.LENGTH_SHORT);
//			toast.show();
//		}
//	}
	// load song data on recent listview
	public class LoadAllMusic extends
			AsyncTask<Void, Void, ArrayList<SongObject>> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			super.onPreExecute();
		}

		@Override
		protected ArrayList<SongObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return audioUltility.getAllSong();
		}

		@Override
		protected void onPostExecute(ArrayList<SongObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			rowAdapter.resetData(result);

		}

	}

	public static RowSongplayallsong getRowSong() {
		return rowAdapter;
	}
}